
//
//  OilMapDetailViewController.m
//  carcareIOS
//
//  Created by wr on 16/4/12.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "OilMapDetailViewController.h"
#import "MapLocationSimple.h"

#import "AlertActionSheetMgr.h"

#import "FuelMgr.h"

#import "OilDetailViewController.h"

#import "OilMapViewController.h"

#define SECTION_HEIGHT (10)
#define CELL_HEIGHT_MAP (12 * 2 + 180)
#define CELL_HEIGHT_BTN (23 * 2 + CELL_HEIGHT)
#define CELL_HEIGHT_BIG (70)
#define CELL_HEIGHT (50)
#define PICKER_VIEW_HEIGHT (261)
typedef NS_ENUM(NSInteger , View_Type) {
    View_Type_TF = 10,//cell 的输入框的tag
    View_Type_Delete_Btn ,//cell上面的删除按钮
    View_Type_TimeL,//map上面的时间label
    View_Type_PlaceL,//map上面加油站地址的label
};

@interface OilMapDetailViewController ()

//地图
@property (nonatomic, strong) MapLocationSimple * mapLoc;

@property (nonatomic, strong) FuelRecordModel * model;

@end

@implementation OilMapDetailViewController
- (instancetype)initWithTableRefreshType:(TYPE_TABLE_REFRESH)type 
{
    self = [super initWithTableRefreshType:type];
    if (self) {
        if([FuelMgr sharedInstance].choiceRecordModel)self.model = [FuelMgr sharedInstance].choiceRecordModel;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"加油明细";
    
    [self setUpRightNavEditBtn];
    
    [self createTableView];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
    
    if (![FuelMgr sharedInstance].choiceRecordModel) {
        [self.navigationController popViewControllerAnimated:YES];
    }

}
- (void)setUpRightNavEditBtn
{
    __weak __typeof(self) wself = self;
    [self navSetupRightTitle:@"编辑" action:^{
        [FuelMgr sharedInstance].choiceRecordModel = wself.model;
        [wself presentViewController:[UINavigationController navigationControllerWithRootViewController:[[OilDetailViewController alloc] initWithInputType:TYPE_INPUT_TABLEVIEW withVCType:VC_Type_CanEdit_Map]] animated:YES completion:nil];

    }];
}
- (void)createTableView
{
    self.tableView.frame = CGRectMake(0, 0 , __MAIN_WIDTH , __MAIN_HEIGHT_64 );
    self.tableView.backgroundColor = [Theme backGroundColor];
    self.tableView.separatorColor = [Theme lineNewColor];
    self.tableView.bounces = (__MAIN_HEIGHT == 480 ? YES : NO);
}
- (NSArray *)titleArray
{
    return @[@[@"空白"],@[@[@"加油金额",@"(元)"],@[@"汽油标号",@""],@[@"汽油单价",@"(元)"],@[@"加油升数",@"(升)"],@[@"加时里程",@"(公里)"]],@[@"地图"],@[@"删除记录"]];
}
#pragma mark - tableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.titleArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.titleArray[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return (indexPath.section == 2 ? CELL_HEIGHT_MAP : (indexPath.section == 3 ?  0 : (indexPath.section == 1 ? (indexPath.row == 0 ? CELL_HEIGHT_BIG : CELL_HEIGHT) : (indexPath.section == 0 ? SECTION_HEIGHT : 0))));
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell;
    CGFloat labelX = 20.5 * __MAIN_RATIO_375;
    switch (indexPath.section) {
            //常规cell
        case 1:
        {
            NSString * cellID = ((indexPath.section == 1 && indexPath.row == 0 ) ? @"cellBigID" : @"cellID");
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                UILabel * leftLabel = [Factory labelWithFrame:CGRectMake(labelX, 0, 115, (indexPath.section == 1 && indexPath.row == 0) ? CELL_HEIGHT_BIG : CELL_HEIGHT) font:FONT_DEFAULT_Light(15) text:@"" textColor:(indexPath.section == 1 ? [Theme wordColor] : COLOR_HEXSTRING(@"#bdbdbd")) onView:cell textAlignment:NSTextAlignmentLeft];
                leftLabel.attributedText = [self titleWithNameStr:self.titleArray[indexPath.section][indexPath.row][0] withUnitStr:self.titleArray[indexPath.section][indexPath.row][1] withUnitFontSize:12 withUnitColor:COLOR_HEXSTRING(@"bdbdbd")];
                
                UITextField * textFied = [Factory textFieldWithFrame:CGRectMake(leftLabel.right, 0, __MAIN_WIDTH - labelX - leftLabel.right, leftLabel.height) hideText:NO defaultText:@"" onView:cell];
                [textFied setTextColor:(indexPath.row == 0 ? COLOR_HEXSTRING(@"#ff3d00") : [Theme wordColor])];
                textFied.font = FONT_DEFAULT_Light((indexPath.section == 1 && indexPath.row == 0) ? 25 : 15);
                textFied.textAlignment = NSTextAlignmentRight;
                textFied.tag = View_Type_TF;
                [textFied setEnabled:NO];
                textFied.keyboardType = UIKeyboardTypeDecimalPad;
                [[[UIView viewWithFrame:CGRectMake(0, leftLabel.bottom - 0.5, __MAIN_WIDTH, 0.5)] resetBackgroundColor:[Theme lineColor]] setupOnView:cell];
            }
            UITextField * inputTF = (UITextField *)[cell viewWithTag:View_Type_TF];
            
            if(indexPath.section == 1)
            {
                switch (indexPath.row) {
                    case 0://加油金额
                        inputTF.text = [NSString stringWithFormat:@"%.02f",self.model.totalPrice];
                        break;
                    case 1://汽油标号
                        
                        inputTF.text = self.model.NEWFUELLABEL;
                        break;
                    case 2://汽油单价
                        inputTF.text = [NSString stringWithFormat:@"%.02f",self.model.FUELPRICE];
                        break;
                    case 3://加油升数
                        inputTF.text = [NSString stringWithFormat:@"%.01f",self.model.fuelVol];
                        break;
                    case 4://加时里程
                        inputTF.text = STRING_INT(self.model.NEWRECORDS);
                        inputTF.text = (![inputTF.text isNotNil]) ? @"未输入车辆里程" : inputTF.text;
                        break;
                    default:
                        break;
                }
                
            }
            
        }
            break;
            
        case 3:
        {
            NSString * cellID = @"btnCellID";
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                cell.backgroundColor = [UIColor clearColor];
                cell.backgroundColor = [Theme backGroundColor];
                //删除按钮
                UIButton * deleteBtn = [ViewFactory buttonWithFrame:CGRectMake(0, CELL_HEIGHT_BTN - CELL_HEIGHT - 23, __MAIN_WIDTH, CELL_HEIGHT)  title:self.titleArray[indexPath.section][indexPath.row] normalColor:[UIColor whiteColor] highlightColor:[[UIColor whiteColor] colorWithAlphaComponent:0.7] disabledColor:[Theme wordGrayColor] inView:cell action:^{

                }];
                deleteBtn.layer.borderColor = [Theme lineColor].CGColor;
                deleteBtn.layer.borderWidth = 0.5;
                [deleteBtn setTitleColor:COLOR_HEXSTRING(@"#ff3d00") forState:UIControlStateNormal];
                [deleteBtn.titleLabel setFont:FONT_DEFAULT_Light(16)];
                deleteBtn.layer.cornerRadius = 0;
                deleteBtn.tag = View_Type_Delete_Btn;
                
            }
            UIButton * deleteBtn = (UIButton *)[cell viewWithTag:View_Type_Delete_Btn];
            [deleteBtn setHidden:YES];
        }
            break;
            //map cell
        case 2:
            
        {
            NSString * cellID = @"mapCellID";
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                cell.backgroundColor = [Theme backGroundColor];
                CGFloat viewY = 12;
                UIView * backView = [[[UIView viewWithFrame:CGRectMake(viewY, viewY  , __MAIN_WIDTH - viewY * 2, CELL_HEIGHT_MAP - viewY * 2)] resetBackgroundColor:[UIColor whiteColor]] setupOnView:cell];
                backView.layer.cornerRadius = 2;
                backView.layer.masksToBounds = YES;
                backView.layer.borderWidth = 0.5;
                backView.layer.borderColor = [Theme lineColor].CGColor;
                CGFloat footerViewH = 65;
                
                __weak __typeof(self) wself = self;
                self.mapLoc = [[MapLocationSimple alloc] init];
                self.mapLoc.mapView.frame = CGRectMake(0, 0, backView.width, backView.height - footerViewH / 2);
                self.mapLoc.mapView.userInteractionEnabled = NO;
                
                self.mapLoc.CalloutViewUpdate = ^(UIView *view, id<MAAnnotation> annotation){

                };
                self.mapLoc.mapDidTouch = ^()
                {
                    [wself.scrollView endEditing:YES];
                };
                [backView addSubview:self.mapLoc.mapView];
                self.mapLoc.coordinate = CLLocationCoordinate2DMake([self.model.latitude doubleValue], [self.model.longitude doubleValue]);
                [self.mapLoc showLocation];
                
                UIView * footerView = [[[UIView viewWithFrame:CGRectMake( 0, backView.height - footerViewH, backView.width , footerViewH)] resetBackgroundColor:[UIColor whiteColor]] setupOnView:backView];
                UIImage * rightImage = ImageNamed(@"cell_right");
                [Factory imageViewWithCenter: CGPointMake(footerView.width - rightImage.size.width / 2 - viewY, footerView.height / 2) image:rightImage onView:footerView];
                UILabel * timeLabel = [Factory labelWithFrame:CGRectMake(viewY, 10, footerView.width - rightImage.size.width - viewY * 2, 20) font:FONT_DEFAULT_Light(13) text:self.model.cdt textColor:COLOR_HEXSTRING(@"#707070") onView:footerView textAlignment:NSTextAlignmentLeft];
                timeLabel.tag = View_Type_TimeL;
                
                UILabel * placeLabel = [Factory labelWithFrame:CGRectMake(timeLabel.left, timeLabel.bottom + 2, timeLabel.width, 22) font:FONT_DEFAULT_Light(17) text:self.model.GSTATION_NAME textColor:COLOR_HEXSTRING(@"#424242") onView:footerView textAlignment:NSTextAlignmentLeft];
                placeLabel.tag = View_Type_PlaceL;
            
            }
            
        }
            break;
        case 0:
            
        {
            NSString * cellID = @"noCellID";
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                cell.backgroundColor = [Theme backGroundColor];
                [[[UIView viewWithFrame:CGRectMake(0, SECTION_HEIGHT - 0.5, __MAIN_WIDTH, 0.5)] resetBackgroundColor:[Theme lineColor]] setupOnView:cell];
            }
        }
            break;
        default:
            break;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //汽油标号点击事件
    if (indexPath.section == 1 && indexPath.row == 1) {
//        [AlertMgr showPopupView:self.oilPickerBackView bottom:0];
    }
    //地图点击事件
    else if(indexPath.section == 2)
    {
        
        [self.navigationController pushViewController:[[[OilMapViewController alloc] initWithMainViewType:TYPE_MAIN_VIEW_NORMAL] navSetupBackItemWithIdentification:BACK_ICON_TITLE] animated:YES];
    }
    
    
}
#pragma mark - 生成使用的attribute str
- (NSMutableAttributedString *)titleWithNameStr:(NSString *)nameStr withUnitStr:(NSString *)unitStr withUnitFontSize:(NSInteger)unitFontSize withUnitColor:(UIColor *)color
{
    NSString * str = [NSString stringWithFormat:@"%@ %@",nameStr, unitStr];
    NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc]initWithString:str];
    if([unitStr length] > 0)
    {
        NSRange range = NSMakeRange([str length] - [unitStr length], [unitStr length]);
        [attributeStr addAttribute:NSFontAttributeName value:FONT_DEFAULT_Light(unitFontSize) range:range];
        if(color)[attributeStr addAttribute:NSForegroundColorAttributeName value:color range:range];
    }
    return attributeStr;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
